package com.msh.artascope.devcloud.service.service.impl;


import com.msh.artascope.devcloud.service.service.EnvService;
import com.msh.artascope.devcloud.service.dao.GitlabConfigDao;
import com.msh.artascope.devcloud.service.model.query.GitlabConfigQueryVo;
import com.msh.artascope.devcloud.service.model.vo.GitlabConfigVo;
import com.msh.frame.client.base.TenantBaseServiceImpl;
import com.msh.frame.common.util.StringUtil;
import javax.annotation.Resource;

import com.msh.frame.interfaces.IdGenerateable;
import com.msh.artascope.devcloud.client.po.GitlabConfigPo;
import com.msh.artascope.devcloud.client.qo.GitlabConfigQo;
import com.msh.artascope.devcloud.service.service.GitlabConfigService;
import com.msh.frame.client.common.Result;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Service
@Slf4j
public class GitlabConfigServiceImpl extends TenantBaseServiceImpl<GitlabConfigPo,GitlabConfigQo> implements GitlabConfigService {

  @Resource
  private EnvService envService;

  @Resource
  private GitlabConfigDao gitlabConfigDao;

  @Resource
  private IdGenerateable idGenerateable;

  @Override
  public long insert(GitlabConfigPo param) {
    param.setId(idGenerateable.getUniqueId());
    return super.insert(param);
  }

  @Override
  public int insertCollection(Collection<GitlabConfigPo> param) {
    param.stream().forEach(p->p.setId(idGenerateable.getUniqueId()));
    return super.insertCollection(param);
  }

  @Override
  public GitlabConfigPo getByEnvId(Long envId, Long tenantId) {
    return listOne(new GitlabConfigQo().setEnvId(envId).setTenantId(tenantId));
  }

  @Override
  public void update(GitlabConfigPo param) {
    Long id = param.getId();
    Long tenantId = param.getTenantId();
    GitlabConfigPo gitlabConfigPo = this.get(id, tenantId);
    String tokenDb = gitlabConfigPo.getToken();
    if(StringUtil.desensitize(tokenDb, 1,4).equals(param.getToken()) || StringUtils.isEmpty(param.getToken())){
      param.setToken(null);
    }
    super.update(param);
  }

  // ----------前端vo处理-------------------------------------------------------

  private GitlabConfigVo toVo(GitlabConfigPo po){
    GitlabConfigVo vo = po.convert(GitlabConfigVo.class);
    vo.setToken(StringUtil.desensitize(vo.getToken(), 1,4));
    return vo;
  }

  private List<GitlabConfigVo> toVoList(List<GitlabConfigPo> list){
    if(CollectionUtils.isEmpty(list)){
      return Collections.emptyList();
    }

    return list.stream().map(po->{
      return toVo(po);
    }).collect(Collectors.toList());
  }

  @Override
  public GitlabConfigVo getVo(Long id, Long tenantId) {
    GitlabConfigPo po = this.get(id, tenantId);
    if(null == po){
      return null;
    }
    GitlabConfigVo vo = toVo(po);

    return vo;
  }

  @Override
  public List<GitlabConfigVo> listVo(GitlabConfigQueryVo q){
    GitlabConfigQo qo = q.convert(GitlabConfigQo.class);
    List<GitlabConfigPo> poList = this.list(qo);
    List<GitlabConfigVo> voList = toVoList(poList);

    return voList;
  }

  @Override
  public Result<List<GitlabConfigVo>> pageVo(GitlabConfigQueryVo param) {
    GitlabConfigQo qo = param.convert(GitlabConfigQo.class);
    int count = this.count(qo);
    if (0 == count) {
      return Result.successReturn(Collections.<GitlabConfigVo>emptyList()).setCount(count);
    }
    List<GitlabConfigPo> poList = this.list(qo);
    List<GitlabConfigVo> voList = toVoList(poList);

    return Result.successReturn(voList).setCount(count);
  }


}
